﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using US.DataAccess;
using US.Entity;
using US.Entity.CommonModule;
using US.Repository;
using US.Utilities;

namespace US.Business
{
    /// <summary>
    /// 出口报关资料维护
    /// <author>
    ///	<name>wx</name>
    ///	<date>2018.12.24 16:54</date>
    /// </author>
    /// </summary>
    public class Bms_ExportBll : RepositoryFactory<Bms_Export>
    {
        /// <summary>
        /// 搜索出口报关单
        /// </summary>
        /// <param name="StartTime">开始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <param name="ParameterJson">查询参数</param>
        /// <param name="jqgridparam">分页参数</param>
        /// <returns></returns>
        public List<Bms_Export> GridListJson(string StartTime, string EndTime, string ParameterJson, ref JqGridParam jqgridparam)
        {
            Bms_ExportDTO dto = new Bms_ExportDTO();

            if (!string.IsNullOrEmpty(ParameterJson))
            {
                dto = ParameterJson.JsonToEntity<Bms_ExportDTO>();
            }
            

            StringBuilder strSql = new StringBuilder();
            List<DbParameter> parameter = new List<DbParameter>();

            strSql.Append(@"SELECT * FROM BMS_EXPORT WHERE 1=1");

            if (!string.IsNullOrEmpty(dto.innerNo))//内部编号
            {
                strSql.Append(" AND INNER_NO like @INNER_NO ");
                parameter.Add(DbFactory.CreateDbParameter("@INNER_NO", '%' + dto.innerNo + '%'));
            }

            if (!string.IsNullOrEmpty(dto.exportInvoice))//出口发票
            {
                strSql.Append(" AND EXPORT_INVOICE like @EXPORT_INVOICE ");
                parameter.Add(DbFactory.CreateDbParameter("@EXPORT_INVOICE", '%' + dto.exportInvoice + '%'));
            }

            if (!string.IsNullOrEmpty(dto.invoiceNO))//发票号码
            {
                strSql.Append(" AND INVOICE_NO like @INVOICE_NO ");
                parameter.Add(DbFactory.CreateDbParameter("@INVOICE_NO", '%' + dto.invoiceNO + '%'));
            }

            if (!string.IsNullOrEmpty(dto.manualNO))//主手册号
            {
                strSql.Append(" AND MANUAL_NO like @MANUAL_NO ");
                parameter.Add(DbFactory.CreateDbParameter("@MANUAL_NO", '%' + dto.manualNO + '%'));
            }

            if (!string.IsNullOrEmpty(dto.ladingBillNO))//主提运单
            {
                strSql.Append(" AND LADING_BILL_NO like @LADING_BILL_NO ");
                parameter.Add(DbFactory.CreateDbParameter("@LADING_BILL_NO", '%' + dto.ladingBillNO + '%'));
            }

            if (!string.IsNullOrEmpty(dto.statusCode))//状态
            {
                strSql.Append(" AND IM_STATUS_CODE = @IM_STATUS_CODE ");
                parameter.Add(DbFactory.CreateDbParameter("@IM_STATUS_CODE",  dto.statusCode ));
            }

            if (!string.IsNullOrEmpty(StartTime) && !string.IsNullOrEmpty(EndTime))//申报日期
            {
                strSql.Append(" AND APPLY_DATE Between @StartTime AND @EndTime ");
                parameter.Add(DbFactory.CreateDbParameter("@StartTime", CommonHelper.GetDateTime(StartTime + " 00:00")));
                parameter.Add(DbFactory.CreateDbParameter("@EndTime", CommonHelper.GetDateTime(EndTime + " 23:59")));
            }

            return Repository().FindListPageBySql(strSql.ToString(), parameter.ToArray(), ref jqgridparam);
        }



    }
}
